//
//  CircleViewController.m
//  Beauty
//
//  Created by Snow on 16/8/29.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "CircleViewController.h"
#import "GXMacro.h"
#import "CircleCollectionViewCell.h"
#import "CircleSecondCollectionViewCell.h"
#import "NewProductViewController.h"
#import "LoginViewController.h"
@interface CircleViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, WishesDelegate, ShoppingDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UIButton *buttonOfShopping;
@property (nonatomic, strong) UIButton *buttonOfWishes;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation CircleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createView];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(handleComplete:)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}
- (void)backToVC
{
    LoginViewController *login = [[LoginViewController alloc]init];
    self.tabBarController.tabBar.hidden = YES;
[self presentViewController:login animated:YES completion:^{
    
}];
}
- (void)wishBackToVC
{
    LoginViewController *login = [[LoginViewController alloc]init];
[self presentViewController:login animated:YES completion:^{
    
}];
}
- (void)handleShopping:(UIButton *)button
{
    _buttonOfWishes.tintColor = [UIColor lightGrayColor];
    _buttonOfShopping.tintColor = [UIColor whiteColor];
    [_collectionView setContentOffset:CGPointMake(0, 0)];
}
- (void)handleWishes:(UIButton *)button
{
    _buttonOfShopping.tintColor = [UIColor lightGrayColor];
    _buttonOfWishes.tintColor = [UIColor whiteColor];
    [_collectionView setContentOffset:CGPointMake(SCREEN_WIDTH, 0)];
}
- (void)handleComplete:(UIBarButtonItem *)barButtonItem
{

}
- (void)createView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    _buttonOfShopping = [UIButton buttonWithType:UIButtonTypeSystem];
    [_buttonOfShopping setTitle:@"购物袋" forState:UIControlStateNormal];
    _buttonOfWishes = [UIButton buttonWithType:UIButtonTypeSystem];
    [_buttonOfWishes setTitle:@"心愿单" forState:UIControlStateNormal];
    _buttonOfShopping.frame = CGRectMake(30, 0, 60, 30);
    _buttonOfWishes.frame = CGRectMake(120, 0, 60, 30);
    [view addSubview:_buttonOfWishes];
    [view addSubview:_buttonOfShopping];
    self.navigationItem.titleView = view;
    [_buttonOfShopping addTarget:self action:@selector(handleShopping:) forControlEvents:UIControlEventTouchUpInside];
    [_buttonOfWishes addTarget:self action:@selector(handleWishes:) forControlEvents:UIControlEventTouchUpInside];
    _buttonOfWishes.tintColor = [UIColor lightGrayColor];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    _collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[CircleCollectionViewCell class] forCellWithReuseIdentifier:@"CircleCollectionViewCell"];
    [_collectionView registerClass:[CircleSecondCollectionViewCell class] forCellWithReuseIdentifier:@"CircleSecondCollectionViewCell"];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        CircleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CircleCollectionViewCell" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    } else {
        CircleSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CircleSecondCollectionViewCell" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_collectionView.contentOffset.x == SCREEN_WIDTH) {
        _buttonOfWishes.tintColor = [UIColor whiteColor];
        _buttonOfShopping.tintColor = [UIColor lightGrayColor];
    } else {
        _buttonOfShopping.tintColor = [UIColor whiteColor];
        _buttonOfWishes.tintColor = [UIColor lightGrayColor];
    }
}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (indexPath.section == 0) {
//        return CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 49);
//    } else {
//        return CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64);
//    }
//}
//wishes delegate
- (void)wishesWithGood_id:(NSString *)good_id
{
    NewProductViewController *new = [[NewProductViewController alloc]init];
    new.goods_id = good_id;
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:new animated:YES];
}
- (void)shopingWithGood_id:(NSString *)string
{
    NewProductViewController *new = [[NewProductViewController alloc]init];
    new.goods_id = string;
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:new animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
